package in.muton.edushare.pages.coursepages;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.auth.roles.PageRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.deptpages.DeptPage;
import in.muton.edushare.pages.univ.UnivPage;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.summary.html.CourseSummary;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.CoursesDAO;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Enrolments;

public abstract class CourseBase extends EduShareBasePage {
	protected static String[] menuItems = { "courseNews", "courseCalendar",
			"folders", "courseStudents", "courseTeachers", "courseEveryone",
			"viewAssn", "courseInvite", "courseUniv", "courseDept",
			"controlPanel", "editProfile", "CoursePoll", "history",
			"CourseGrading", "attendance"};

	public CourseBase(PageParameters params) {
		super();
		add(CSSPackageResource.getHeaderContribution(CourseBase.class,
				"courseHome.css"));
		getCourse(params);
		add(new Label("courseName", new PropertyModel<String>(getCourse(),
				"courseName")));
		String s = getCourseInstance().getObject().getSemester()
				.getSemesterName()
				+ ", " + getCourseInstance().getObject().getInstanceYear();
		add(new Label("semName", new Model<String>(s)));
		Link<Void> l = new BookmarkablePageLink<Void>("deptLink",
				DeptPage.class, EntityPageParams.getParams(cd.getOwner()));
		add(l);
		l.add(new Label("deptName", cd.getOwner().getObject().getDeptName()));

		l = new BookmarkablePageLink<Void>("univLink", UnivPage.class,
				EntityPageParams.getParams(cd.getOwnerUniv()));
		add(l);
		l.add(new Label("univName", cd.getOwnerUniv().getObject()
				.getUniversityName()));
		add(new BookmarkablePageLink<Void>("vwProfile", URLLoader
				.getPageClass(URLConstants.COURSE_PUBLIC_PAGE_HOME),
				EntityPageParams.getParams(getCourse())));
		add(new Link<Void>("printReport"){

			private static final long serialVersionUID = -3323859770228433006L;

			@Override
			public void onClick() {
				setResponsePage(new CourseSummary(courseInstance));
		}});
		addMenuItems(params);
	}

	private void addMenuItems(PageParameters params) {
		String highlight = getActivePage(params);
		CourseRoleCheck crc = new CourseRoleCheck(courseInstance.getObject(),
				getLoggedInUser());
		for (String s : menuItems) {
			WebMarkupContainer wmc;
			add(wmc = new WebMarkupContainer(s));
			if (highlight.equals(s)) {
				wmc.add(new AttributeAppender("class", new Model<String>(
						"es-selected"), " "));
			}
			if (s.equals("courseNews")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CoursePage.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
			} else if (s.equals("viewAssn")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CourseAssignmentList.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));

			} else if (s.equals("folders")) {
				PageParameters folderParams = EntityPageParams.getParams(
						getCourse(), courseInstance.getObject()
								.getCourseInstanceId().toString());
				folderParams.add("f", "0");
				wmc.add(new BookmarkablePageLink<Void>("link",
						CourseFolder.class, folderParams));
			} else if (s.equals("courseCalendar")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CourseCalendar.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
			} else if (s.equals("courseStudents")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CoursePeople.class, EntityPageParams.getParams(
								getCourse(), new String[] {
										courseInstance.getObject()
												.getCourseInstanceId()
												.toString(), s })));
			} else if (s.equals("courseTeachers")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CoursePeople.class, EntityPageParams.getParams(
								getCourse(), new String[] {
										courseInstance.getObject()
												.getCourseInstanceId()
												.toString(), s })));
			} else if (s.equals("courseEveryone")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CoursePeople.class, EntityPageParams.getParams(
								getCourse(), new String[] {
										courseInstance.getObject()
												.getCourseInstanceId()
												.toString(), s })));
			} else if (s.equals("courseInvite")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CourseInvite.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
				wmc.setVisible(crc.getRole() == Enrolments.TEACHER
						|| crc.isAdmin());
			} else if (s.equals("courseUniv")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CourseUniv.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
			} else if (s.equals("courseDept")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CourseDept.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
			} else if (s.equals("controlPanel")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CoursePage.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
				wmc.setVisible(crc.getRole() == Enrolments.TEACHER
						|| crc.isAdmin());
				wmc.setVisible(false);
			} else if (s.equals("editProfile")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CourseEdit.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
				wmc.setVisible(crc.getRole() == Enrolments.TEACHER
						|| crc.isAdmin());
			} else if (s.equals("CoursePoll")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CoursePolls.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
				wmc.setVisible(crc.getRole() == Enrolments.TEACHER
						|| crc.isAdmin());
			} else if (s.equals("history")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CoursePrevInstances.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
			} else if (s.equals("CourseGrading")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						CourseGrading.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
			} else if (s.equals("attendance")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						Attendance.class, EntityPageParams.getParams(
								getCourse(), courseInstance.getObject()
										.getCourseInstanceId().toString())));
			}
		}
	}

	protected abstract String getActivePage(PageParameters params);

	private void getCourse(PageParameters params) {
		String id = params.getString("1");
		if (StringUtil.isValid(id)) {
			Courses course = new CoursesDAO(getSessionORMInterface())
					.getCourseById(Integer.parseInt(id));
			if (course != null) {
				setCourse(new CayenneModel<Courses>(course));
				if (StringUtil.isValid(params.getString("2"))) {
					CourseInstances cI = new CourseInstancesDAO(
							getSessionORMInterface())
							.getCourseInstanceById(Integer.parseInt(params
									.getString("2")));
					if (cI == null) {
						WebPage p = new CourseUnavailable(params);
						throw new RestartResponseAtInterceptPageException(p);
					}
					setCourseInstance(new CayenneModel<CourseInstances>(cI));
					cd = new CourseDataHandler(getCourseInstance());
					prc = new PageRoleCheck(cI, getLoggedInUser());
					return;
				} else {
					CourseInstances cI = new CourseInstancesDAO(
							getSessionORMInterface()).getActiveInst(course);
					if (cI == null) {
						WebPage p = new CourseUnavailable(params);
						throw new RestartResponseAtInterceptPageException(p);
					}
					setCourseInstance(new CayenneModel<CourseInstances>(cI));
					cd = new CourseDataHandler(getCourseInstance());
					prc = new PageRoleCheck(cI, getLoggedInUser());
					return;
				}
			}
		}
		throw new EduShareRuntimeException("Unable to find courses");
	}

	@Override
	public String getUserRole() {
		return " : " + cd.getDeptRole(EduShareSession.get().getLoggedInUser());
	}

	public void setCourseInstance(IModel<CourseInstances> courseInstance) {
		this.courseInstance = courseInstance;
	}

	public IModel<CourseInstances> getCourseInstance() {
		return courseInstance;
	}

	public void setCourse(IModel<Courses> course) {
		this.course = course;
	}

	public IModel<Courses> getCourse() {
		return course;
	}

	private IModel<Courses> course;
	private IModel<CourseInstances> courseInstance;
	private CourseDataHandler cd = null;
}
